package study.designpattern.adapter;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import study.designpattern.adapter.bean.LogModel;
import study.designpattern.adapter.impl.LogDbOperate;
import study.designpattern.adapter.impl.LogFileOperate;
import study.designpattern.adapter.impl.TwoDirectAdapter;


/**
 * 客户端类，作测试用
 * @author taowei20061122@163.com
 *
 */
public class Client {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		//准备日志内容
		LogModel logModel = new LogModel();
		
		logModel.setLogId("logId_03");
		logModel.setOperateUser("hawk02");
		logModel.setOperateTime("2012-09-01 14:23:19");
		logModel.setLogContent("A this is a test!!!");
		
		List<LogModel> list = new ArrayList<LogModel>();
		list.add(logModel);
		
/*		//---------第一版
		//创建文件操作的对象
		LogFileOperateApi api = new LogFileOperate("");
		
		//保存日志文件
		api.writeLogFile(list);
		
		//读取日志文件的内容
		List<LogModel> readLogs = api.readLogFile();
		System.out.println("readLogs="+readLogs);
		//---------第一版
*/
		
		/*
		//---------第二版
		//创建文件操作的对象
		LogFileOperateApi logFileApi = new LogFileOperate("");
		
		//创建新版操作日志的接口对象
		LogDbOperateApi api = new Adapter(logFileApi);
		
		//保存日志文件
		api.createLog(logModel);
		
		//读取日志文件内容
		List<LogModel> allLogs = api.getAllLog();
		
		System.out.println("allLogs="+allLogs);
		*/
		
		//--------双向适配		
		//创建操作日志的对象
		LogFileOperateApi logFileOperate = new LogFileOperate("");
		LogDbOperateApi logDbOperate = new LogDbOperate();
		//LogFileOperate logFileOperate = new LogFileOperate("");
		//LogDbOperate logDbOperate = new LogDbOperate();
		
		//创建经过双向适配的操作日志的对象
		LogFileOperateApi logFileOperate2 = new TwoDirectAdapter(logFileOperate, logDbOperate);
		LogDbOperateApi logDbOperate2 = new TwoDirectAdapter(logFileOperate, logDbOperate);
		
		//从文件适配到第二版
		//虽然调用的是Db的接口，其实内部是文件操作
		logDbOperate2.createLog(logModel);		
		List<LogModel> readLogs = logDbOperate2.getAllLog();
		System.out.println("readLogs="+readLogs);
		
		//测试从Db适配到第一版的接口
		//虽然调用的是文件操作的接口，其实内部是Db存储操作
		logFileOperate2.writeLogFile(readLogs);
		logFileOperate2.readLogFile();
		
	}

}
